<?php


namespace App\Http\Controllers;


use App\Models\Filter;
use QL\QueryList;

class QueryController
{
    public function pp()
    {
        //Filter::resourceLink('https://www.szweb.cn/dianshangcase/');
        $site = 'https://www.szweb.cn';
        $son_cate = QueryList::get('https://www.szweb.cn/weixinxiaochengxu/')
            // 设置采集规则
            ->rules([
                'title'=>array('dd>a','text'),
                'a_href'=>array('dd>a','href'),
            ])
            ->query()->getData()->toArray();
        //print_r($son_cate);
        $son_cate = array_slice($son_cate, 7);
        //print_r($son_cate);die();
        $data = [];
        $son_cate = [
            ['title'=> '联系方式', 'a_href'=> 'https://www.szweb.cn/contact/'],
            ['title'=> '人才招聘', 'a_href'=> 'https://www.szweb.cn/jobs/'],
        ];
        //print_r($son_cate);die();
       /* foreach($son_cate as $val){
            $ql = QueryList::get($val['a_href']);
            $data['title'] =  $ql->find('title')->text(); // 获取网站标题
            $data['keywords'] =  $ql->find('meta[name=keywords]')->content; // 获取网站头部关键词 // 获取网站标题
            //echo Filter::str_replace($keywords);die();
            $data['description'] = $ql->find('meta[name=description]')->content; //
            Filter::createCate($val['title'], $data);
        }*/

        foreach($son_cate as $key=>$val){
            //route
            $filename = pathinfo($val['a_href'],PATHINFO_FILENAME);
            $str = strstr($val['a_href'],'cn/');
            $arr = explode( '/', $str);
            //echo $str.PHP_EOL;
            if(empty($arr[2])){
             //  echo 'Route::get(\'/'.$arr['1'].'\', \'ContactController@'.$arr['1'].'\');'.PHP_EOL;
            }
            //action
            if(empty($arr[2])) {
                $str = '
            public function '.$arr[1].'()
            {
                $catelist = Category::getCateListBy(\''.$arr[1].'\');
                Filter::resourceLink(\'https://www.szweb.cn/'.$arr[1].'\');
                Filter::cutStr(
                    \'https://www.szweb.cn/'.$arr[1].'\',
                    \'<div class="full-site-article">\',
                    \'<div class="full-related-news">\',
                    \'contact/'.$arr[1].'\'
                );
                $cate = Category::getCateByTargetName("'.$arr[1].'");
                return view("contact.'.$arr[1].'",compact("cate", "catelist"));
            }';
                echo $str.PHP_EOL.PHP_EOL;
            }
        }
    }

    public function test()
    {
        //$domain = 'https://www.szweb.cn';
        $ql = QueryList::get('https://www.szweb.cn/wangzhanjianshe/');
        $title =  $ql->find('title')->text(); // 获取网站标题
        $keywords =  $ql->find('meta[name=keywords]')->content; // 获取网站头部关键词 // 获取网站标题
        //echo Filter::str_replace($keywords);die();
        echo $description =  $ql->find('meta[name=description]')->content; // 获取网站头部关键词 // 获取网站标题
        //$res = $ql->find('.full-dropdown>div>h4[class=en]')->texts();
        //$res = json_decode($res,true);
        //print_r($res);
        //echo $res;
        /*$img_src =  $ql->find('img')->eq(1)->src;
        $cate_logo_url = $domain.$img_src;
        $filename = Filter::download($cate_logo_url);
        echo $filename;*/
       //echo $cate_logo_url;die();
        //die();

        /*$site = 'https://www.szweb.cn';
        //导航分类
        $index_cate = QueryList::get('https://www.szweb.cn')
            // 设置采集规则
            ->rules([
                'name'=>array('.nav>ul>li>h3>a','text'),
                'a_href'=>array('.nav>ul>li>h3>a','href'),
            ])
            ->query()->getData()->toArray();
        $data = [];
        foreach ($index_cate as $key=>&$val) {
            if($val['a_href'] == '/'){
                $val['a_href'] = 'https://www.szweb.cn';
            }
            $ql =  QueryList::get($val['a_href']);
            $data[$key]['name'] = $val['name'];
            $data[$key]['title'] = $ql->find('title')->text();
            $data[$key]['keywords'] =  $ql->find('meta[name=keywords]')->content;
            $data[$key]['description'] =  $ql->find('meta[name=description]')->content;
            Filter::createCate($val['name'], $data[$key]);
        }*/

        //die();
        //网站建设和电商网站开发子分类
        $site = 'https://www.szweb.cn';
        $son_cate = QueryList::get('https://www.szweb.cn/wangzhanjianshe/')
            // 设置采集规则
            ->rules([
                'title'=>array('.m-ico-links>a>span','text'),
                'a_href'=>array('.m-ico-links>a','href'),
                'img_src'=>array('.m-ico-links>a>img','src'),
            ])
            ->query()->getData()->toArray();
        //print_r($son_cate);
        $son_cate = array_slice($son_cate, 12);
        foreach($son_cate as $key=>$val){
            //route
           $filename = pathinfo($val['a_href'],PATHINFO_FILENAME);
            $str = strstr($val['a_href'],'cn/');
            $arr = explode( '/', $str);
            //echo $str.PHP_EOL;
            /* if(!empty($arr[2])){
                echo 'Route::get(\'/'.$arr['1'].'/'.$arr['2'].'\', \'BuildController@'.$arr['2'].'\');'.PHP_EOL;
            }
            if(empty($arr[2])){
                echo 'Route::get(\'/'.$arr['1'].'\', \'BuildController@'.$arr['1'].'\');'.PHP_EOL;
            }*/
            //电商
            if(empty($arr[2])){
                //echo 'Route::get(\'/'.$arr['1'].'\', \'DianshangController@'.$arr['1'].'\');'.PHP_EOL;
            }
            //action
            if(empty($arr[2])) {
                $str = '
            public function '.$arr[1].'()
            {
                $catelist = Category::getCateListBy(\''.$arr[1].'\');
                Filter::resourceLink(\'https://www.szweb.cn/'.$arr[1].'\');
                Filter::cutStr(
                    \'https://www.szweb.cn/'.$arr[1].'\',
                    \'<div class="full-site-article">\',
                    \'<div class="full-related-news">\',
                    \'dianshangcase/'.$arr[1].'\'
                );
                $cate = Category::getCateByTargetName("'.$arr[1].'");
                return view("dianshangcase.'.$arr[1].'",compact("cate", "catelist"));
            }';
              //  echo $str.PHP_EOL.PHP_EOL;
            }
            if(empty($arr[2])){

            }
        }
        //die();
        /*$data = [];
        foreach ($son_cate as $key=>$val) {
            $ql =  QueryList::get($val['a_href']);
            $data[$key]['name'] = $val['title'];
            $data[$key]['long_name'] = $ql->find('.full-case-crumbs>.weblg>.crumbs>span')->text();
            $data[$key]['title'] = $ql->find('title')->text();
            $data[$key]['keywords'] =  $ql->find('meta[name=keywords]')->content;
            $data[$key]['description'] =  $ql->find('meta[name=description]')->content;
            $data[$key]['img'] = Filter::download($site.$val['img_src']);
            Filter::createCate($val['title'], $data[$key]);
        }*/
        //print_r($son_cate);
        //方案
        $site = 'https://www.szweb.cn';
        $son_cate = QueryList::get('https://www.szweb.cn/solution/')
            // 设置采集规则
            ->rules([
                'title'=>array('.full-dropdown>div>div>ul>li>a','text'),
                'a_href'=>array('.full-dropdown>div>div>ul>li>a','href'),
                //'img_src'=>array('.m-ico-links>a>img','src'),
            ])
            ->query()->getData()->toArray();
        //print_r($son_cate);die();
        foreach($son_cate as $key=>$val){
            //route
            $filename = pathinfo($val['a_href'],PATHINFO_FILENAME);
            $str = strstr($val['a_href'],'cn/');
            $arr = explode( '/', $str);
            //echo $str.PHP_EOL;
            if(!empty($arr[2])){
                //echo 'Route::get(\'/'.$arr['1'].'/'.$arr['2'].'\', \'SolutionController@'.$arr['2'].'\');'.PHP_EOL;
            }
            if(empty($arr[2])){
               // echo 'Route::get(\'/'.$arr['1'].'\', \'SolutionController@'.$arr['1'].'\');'.PHP_EOL;
            }
            //action
            if(!empty($arr[2])) {
                $str = '
            public function '.$arr[2].'()
            {
                $catelist = Category::getCateListBy(\''.$arr[1].'\');
                Filter::cutStr(
                    \'https://www.szweb.cn/'.$arr[1].'/'.$arr[2].'/\',
                    \'<div class="full-case-container">\',
                    \'<div class="full-related-news">\',
                    \'build/'.$arr[2].'\'
                );
                $cate = Category::getCateByTargetName("'.$arr[2].'");
                return view("'.$arr['1'].'.'.$arr[2].'",compact("cate", "catelist"));
            }';
               // echo $str.PHP_EOL.PHP_EOL;
            }
            if(empty($arr[2])){

            }
        }
        //print_r($son_cate);die();
       /* $data = [];
        foreach ($son_cate as $key=>$val) {
            $ql =  QueryList::get($val['a_href']);
            $data[$key]['name'] = $val['title'];
            $data[$key]['long_name'] = $ql->find('.full-case-crumbs>.weblg>.crumbs>span')->text();
            $data[$key]['title'] = $ql->find('title')->text();
            $data[$key]['keywords'] =  $ql->find('meta[name=keywords]')->content;
            $data[$key]['description'] =  $ql->find('meta[name=description]')->content;
            //$data[$key]['img'] = Filter::download($site.$val['img_src']);
            Filter::createCate($val['title'], $data[$key]);
        }
        print_r($data);*/
        //微信小程序 咨询
        $site = 'https://www.szweb.cn';
        $son_cate = QueryList::get('https://www.szweb.cn/weixinxiaochengxu/')
            // 设置采集规则
            ->rules([
                'title'=>array('.full-dropdown>.nav-web>ul>li>h3>a','text'),
                'a_href'=>array('.full-dropdown>.nav-web>ul>li>h3>a','href'),
                'img_src'=>array('.full-dropdown>.nav-web>ul>li>a>img','src'),
            ])
            ->query()->getData()->toArray();

        //print_r($son_cate);die();
        $son_cate = array_slice($son_cate, 0,4);

        foreach($son_cate as $key=>$val){
            //route
            $filename = pathinfo($val['a_href'],PATHINFO_FILENAME);
            $str = strstr($val['a_href'],'cn/');
            $arr = explode( '/', $str);
            //echo $str.PHP_EOL;
             if(!empty($arr[2])){
                //echo 'Route::get(\'/'.$arr['1'].'/'.$arr['2'].'\', \'BuildController@'.$arr['2'].'\');'.PHP_EOL;
            }
            if(empty($arr[2])){
               // echo 'Route::get(\'/'.$arr['1'].'\', \'WxappController@'.$arr['1'].'\');'.PHP_EOL;
            }
            //电商
            if(empty($arr[2])){
                //echo 'Route::get(\'/'.$arr['1'].'\', \'DianshangController@'.$arr['1'].'\');'.PHP_EOL;
            }
            //action
            if(empty($arr[2])) {
                $str = '
            public function '.$arr[1].'()
            {
                $catelist = Category::getCateListBy(\''.$arr[1].'\');
                Filter::resourceLink(\'https://www.szweb.cn/'.$arr[1].'\');
                Filter::cutStr(
                    \'https://www.szweb.cn/'.$arr[1].'\',
                    \'<div class="full-site-article">\',
                    \'<div class="full-related-news">\',
                    \'wxapp/'.$arr[1].'\'
                );
                $cate = Category::getCateByTargetName("'.$arr[1].'");
                return view("wxapp.'.$arr[1].'",compact("cate", "catelist"));
            }';
                 echo $str.PHP_EOL.PHP_EOL;
            }
            if(empty($arr[2])){

            }
        }
        die();
        $data = [];
        foreach ($son_cate as $key=>$val) {
            $ql =  QueryList::get($val['a_href']);
            $data[$key]['name'] = $val['title'];
            $data[$key]['long_name'] = $ql->find('.full-case-crumbs>.weblg>.crumbs>span')->text();
            $data[$key]['title'] = $ql->find('title')->text();
            $data[$key]['keywords'] =  $ql->find('meta[name=keywords]')->content;
            $data[$key]['description'] =  $ql->find('meta[name=description]')->content;
            $data[$key]['img'] = Filter::download($site.$val['img_src']);
            Filter::createCate($val['title'], $data[$key]);
        }
        print_r($data);
    }
}